package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.UserCollectAdapter;
import com.dlwx.firefighting.bean.UserCollectBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的收藏界面
 * wuconghui
 */
public class UserCollectActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.collect_empty_view)
    AutoLinearLayout emptyView;

    private LRecyclerView recyclerView;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private UserCollectAdapter adapter;
    private int TOTAL_COUNTER = 7;
    private static final int REQUEST_COUNT = 10;
    private static int mCurrentCounter = 0;
    private int page=1;
    private int requestNum=10;
    private int count=0;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_collect);
        ButterKnife.bind(this);
        userId= (String) SPUtils.get(this, Constant.USER_ID,"");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText(getResources().getString(R.string.my_collect));

        initRecyView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void getDatas() {
//        收藏类型（1、商品2、音视频）
        OkGo.<String>post(Constant.MY_COLLECT)
                .tag(this)
                .params("user_id", userId)
//                .params("type", 0)
                .params("page", page)
                .params("num", requestNum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<UserCollectBean> beans = dealResult.deal(UserCollectActivity.this, response, new TypeToken<BaseBean<UserCollectBean>>() {
//                        }.getType());
//                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(UserCollectActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<UserCollectBean> beans) {
      /*  if(beans.isEmpty()){
            return;
        }*/
        adapter.addAll(beans);
        recyclerView.refreshComplete(requestNum);
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyView() {
        recyclerView = (LRecyclerView) findViewById(R.id.collect_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UserCollectAdapter(UserCollectActivity.this);
        adapter.setDataList(new ArrayList<UserCollectBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);

        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.size_1dp)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.app_bg_color)
                .build();

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        //设置上拉加载更多
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if(count<(page*requestNum)){
                    ToastUtils.toastForShort(UserCollectActivity.this,getString(R.string.no_more));
                    recyclerView.setNoMore(true);
                }else{
                    page++;
                    getDatas();

                }
            }
        });
        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        //初始化数据

        //删除收藏
        adapter.setOnItemDeleteListener(new UserCollectAdapter.OnItemDeleteListener() {
            @Override
            public void OnDelete(int position) {
                String id =adapter.getDataList().get(position).getCollect_id();
                doDelect(id);
            }
        });
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                UserCollectBean bean = adapter.getDataList().get(position);
                Intent i=new Intent();
                //                  "type": 1//  1、商品 2、视频 3、音频
                switch (bean.getType()){
                    case 1:
                        i.putExtra("id",bean.getGoods_id());
                        i.setClass(UserCollectActivity.this, GoodsDetailsActivity.class);
                        break;
                    case 2:
                        i.putExtra("id",bean.getVideo_id());
                        i.setClass(UserCollectActivity.this, VideoDetailActivity.class);
                        break;
                    case 3:
                        i.putExtra("id",bean.getVideo_id());
                        i.setClass(UserCollectActivity.this, VideoDetailActivity.class);
                        break;
                }
                startActivity(i);
            }
        });

    }

    private void doDelect(String collectId) {
        DialogUtils.showProgressDlg(this,"");
        OkGo.<String>post(Constant.DELECT_COLLECT)
                .tag(this)
                .params("user_id", userId)
                .params("type", 1)
                .params("collect_id", collectId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                         BaseBean bean = dealResult.dealBaseRes(UserCollectActivity.this, response);
//                        if(bean!=null){
//                            if(bean.getStatus()==Constant.SUCESS){
//                                adapter.clear();
//                                lRecyclerViewAdapter.notifyDataSetChanged();
//                                getDatas();
//                            }
//                            ToastUtils.toastForShort(UserCollectActivity.this,bean.getMsg());
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(UserCollectActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    @OnClick(R.id.title_left_one_btn)
    public void onViewClicked() {
        finish();
    }

    /**
     * 刷新
     */
    private void refresh() {
        adapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        page=1;
        getDatas();
    }


    //-------------------------------------------------------------
}
